/**
 * 
 */
package net.bingosoft.common.upload;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 文件上传参数配置
 * @author elvis
 */
public class UploadConfig {
    
    private static final Logger log = LoggerFactory.getLogger(UploadConfig.class);
    
    public static final String CACHE_TIMEOUT   = "upload_cache_timeout";
    public static final String ALLOW_EMPTY     = "upload_allow_empty";
    public static final String MAX_FILE_SIZE   = "upload_max_file_size";
    public static final String UPLOAD_FOLDER   = "upload_folder";
    

    /** 文件信息存储超时时间，单位秒，默认120分钟 */
    private int timeout = 2 * 60 * 60;
    
    /** 是否允许空文件上传 */
    private boolean allowEmpty = false;
    
    /** 最大上传文件大小，单位字节,默认为100M，小于等于0为无限制 */
    private long maxFileSize = 100 * 1024 * 1024;
    
    /** 字符编码 */
    private String encoding = "UTF-8";
    
    /** 上传文件存储目录，如果没有指定则存储在临时目录:System.getProperty("java.io.tmpdir") */
    private String uploadFolder;
    
    private IUploadPolicy uploadPolicy;
    
    public int getTimeout() {
        return timeout;
    }

    public void setTimeout(int timeout) {
        this.timeout = timeout;
    }
    
    public boolean isAllowEmpty() {
        return allowEmpty;
    }

    public void setAllowEmpty(boolean allowEmpty) {
        this.allowEmpty = allowEmpty;
    }
    
    public String getEncoding() {
        return encoding;
    }

    public void setEncoding(String encoding) {
        this.encoding = encoding;
    }

    public String getUploadFolder() {
        if(null == this.uploadFolder){
            this.uploadFolder = System.getProperty("java.io.tmpdir").replaceAll("\\\\", "/");
        }
        
        if(!uploadFolder.endsWith("/")){
            uploadFolder += "/";
        }
        return uploadFolder;
    }

    public void setUploadFolder(String uploadFolder) {
        if(null != uploadFolder){
            this.uploadFolder = uploadFolder.replaceAll("\\\\", "/");
        }
    }

    public IUploadPolicy getUploadPolicy() {
        if(null == uploadPolicy){
            uploadPolicy = new UploadPolicy();
        }
        return uploadPolicy;
    }

    public void setUploadPolicy(IUploadPolicy uploadPolicy) {
        this.uploadPolicy = uploadPolicy;
    }

    public long getMaxFileSize() {
        return maxFileSize;
    }

    public void setMaxFileSize(long maxUploadSize) {
        this.maxFileSize = maxUploadSize;
    }
    
    public void config(String name,String value){
        log.debug("config upload parameter key:{},value:{}",name,value);
        try {
            if(name.equals(UploadConfig.ALLOW_EMPTY)){
                this.setAllowEmpty(Boolean.valueOf(value));
            }else if(name.equals(UploadConfig.CACHE_TIMEOUT)){
                this.setTimeout(Integer.parseInt(value));
            }else if(name.equals(UploadConfig.MAX_FILE_SIZE)){
                this.setMaxFileSize(Long.parseLong(value));
            }else if(name.equals(UploadConfig.UPLOAD_FOLDER)){
                this.setUploadFolder(value.trim());
            }else{
                log.debug("{} is not a upload parameter,ignore it",name);
            }
        } catch (Exception e) {
            if(log.isWarnEnabled()){
                log.warn("invalid config value {} of {},excepiton:{}",new Object[]{value,name,e.getMessage()});
            }
        }
    }
    
    public void copyFrom(UploadConfig src){
        this.setAllowEmpty(src.isAllowEmpty());
        this.setTimeout(src.getTimeout());
        this.setEncoding(src.getEncoding());
        this.setMaxFileSize(src.getMaxFileSize());
        this.setUploadFolder(src.getUploadFolder());
        this.setUploadPolicy(src.getUploadPolicy());
    }
    
    public static boolean isConfigField(String fieldName){
        return CACHE_TIMEOUT.equals(fieldName) || ALLOW_EMPTY.equals(fieldName) 
                    || MAX_FILE_SIZE.equals(fieldName) || UPLOAD_FOLDER.equals(fieldName);
    }
}
